//
//  FLPresentationController.swift
//  swift_learning
//
//  Created by aron on 2017/1/25.
//  Copyright © 2017年 arons. All rights reserved.
//

import UIKit

class FLPresentationController: UIPresentationController {

    // MARK:- 对外提供属性
    var presentedFrame : CGRect = CGRect.zero

    // MARK:- 懒加载属性
    private lazy var coverView : UIView = UIView()

    override func containerViewWillLayoutSubviews() {
        super.containerViewWillLayoutSubviews()
        
        // 1.设置弹出View的尺寸
        presentedView?.frame = presentedFrame

        setupCoverView()
    }
    
    private func setupCoverView() {
        // 1.添加蒙版
        containerView?.insertSubview(coverView, at: 0)
        
        // 2.设置蒙版的属性
        coverView.backgroundColor = UIColor(white: 0.8, alpha: 0.2)
        coverView.frame = containerView!.bounds
        
        // 3.添加手势
        let tapGes = UITapGestureRecognizer(target: self, action: #selector(FLPresentationController.coverViewClick))
        coverView.addGestureRecognizer(tapGes)
    }
    
    @objc private func coverViewClick() {
        presentedViewController.dismiss(animated: true, completion: nil)
    }
}
